package com.xm.transfer.utils;

import com.xm.transfer.annotations.Autowired;
import com.xm.transfer.annotations.Component;

import java.sql.SQLException;

@Component
public class TransactionManager {

    private static TransactionManager transactionManager = new TransactionManager();


    @Autowired
    private ConnectionUtils connectionUtils;

    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }

    public static TransactionManager getInstance() {
        if (null == transactionManager) {
            synchronized (TransactionManager.class) {
                if (null == transactionManager) {
                    transactionManager = new TransactionManager();
                }
            }
        }
        return transactionManager;
    }

    public void enableTransaction() throws SQLException {
        connectionUtils.getCurrentThreadConnection().setAutoCommit(false);
    }

    public void commitTransaction() throws SQLException {
        connectionUtils.getCurrentThreadConnection().commit();
    }

    public void rollbackTransaction() throws SQLException {
        connectionUtils.getCurrentThreadConnection().rollback();
    }


}
